from django.shortcuts import render,redirect
from django.http.response import HttpResponse,JsonResponse
from json import dumps




def session_demo(request):
    request.session['name'] = 'python'
    print(request.session.get('name'))

    return HttpResponse('session_demo')





def demo_view1(request):
    response = HttpResponse('ok')
    response.set_cookie('name','tom',max_age=100)
    # print(request.COOKIES.get('name'))

    return response




def demo_view(request):

    # ???
    return redirect('index.html')






def json_response(request):
    json_dict = {"name":"tony","age":18}
    json_dict = dumps(json_dict)
    # return JsonResponse(json_dict)
    return HttpResponse(json_dict, content_type='application/json')

        #     if json_dumps_params is None:
        #     json_dumps_params = {}
        # kwargs.setdefault('content_type', 'application/json')
        # data = json.dumps(data, cls=encoder, **json_dumps_params)
        # super(JsonResponse, self).__init__(content=data, **kwargs)





# HttpResponse(content=响应体, content_type=响应体数据类型, status=状态码)
def response_demo(request):

    response =  HttpResponse('response_demo',content_type='text/html',status= 201)
    response['hello'] = 'python'
    return response









def get_request_head(request):
    print(request.META['CONTENT_TYPE'])
    print(request.method)
    print(request.user)


    return HttpResponse('get_request_head')



def get_form(request):

    like = request.POST.get('like')
    b = request.POST.get('b')
    like_list = request.POST.getlist('like_list')



    return HttpResponse('get_form')



def get_query_string(request):
    a = request.GET.get('a')
    b = request.GET.get('b')
    a_list = request.GET.getlist('a')

    print(a,b,a_list)



    return HttpResponse('get_query_string')




def weather2(request,city,year):
    print(city)
    print(year)


    return HttpResponse('ok')


# Create your views here.
# /weather/beijing/2018
def weather1(request,city,year):
    print(city)
    print(year)


    return HttpResponse('ok')